<?php
// +----------------------------------------------------------------------
// | B5LaravelCMF
// +----------------------------------------------------------------------
// | Author: 李恒 <357145480@qq.com>
// +----------------------------------------------------------------------
namespace App\Services;

use App\Helpers\Util\IpLocation\IpLocation;
use App\Models\Loginlog;
use Illuminate\Support\Facades\Request;
use Jenssegers\Agent\Agent;

/**
 * 登录日志
 * Class Loginlog
 * @package App\Services
 */
class LoginlogService extends BaseService
{
    public function __construct(bool $loadValidate = true)
    {
        $this->setModel(new Loginlog());
    }

    public function getList($all = false)
    {
        return parent::getList($all, [], [['id', 'desc']]); // TODO: Change the autogenerated stub
    }

    public function logAdd($login_name, $status, $msg)
    {
        $agent = new Agent();
        $os = $agent->platform() . ' ' . $agent->version($agent->platform());
        $browser = $agent->browser() . ' ' . $agent->version($agent->browser());
        $login_time = date('Y-m-d H:i:s', time());
        $ipaddr = Request::ip();
        $login_location = '';
        $net='';
        if ($ipaddr) {
            $ipLocation = new IpLocation();
            $location = $ipLocation->getlocation($ipaddr);
            if ($location){
                if($location['country']) {
                    $login_location = iconv('GBK', 'UTF-8', $location['country']);
                }
                if($location['area']) {
                    $net = iconv('GBK', 'UTF-8', $location['area']);
                }
            }
        }
        $this->add(['login_name' => $login_name, 'ipaddr' => $ipaddr, 'browser' => $browser, 'os' => $os, 'status' => $status, 'msg' => $msg, 'login_time' => $login_time, 'login_location' => $login_location,'net'=>$net]);
    }
}
